#include "Renderer.h"
#ifdef WIN32
#include <windows.h>
#endif

#include <GL/glfw.h>
#include <GL/glut.h>

#include <iostream>



AI_SOKOBAN::Renderer::Renderer(int x, int y)
{
    m_windowHeight = y*32;
    m_windowWidth = x*32;
    initGL();
}

void AI_SOKOBAN::Renderer::initGL()
{
    if( !glfwInit() )
    {
        exit( EXIT_FAILURE );
    }

    glfwSetWindowTitle("The GLFW Window");
    if(!glfwOpenWindow(m_windowWidth,m_windowHeight,0,0,0,0,0,0, GLFW_WINDOW))
    {

        glfwTerminate();
        exit( EXIT_FAILURE );
    }
}

void AI_SOKOBAN::Renderer::reshape(int w, int h) 
{
    m_windowWidth = w;
    m_windowHeight = h;
    glViewport(0.0f, 0.0f, (GLsizei)m_windowWidth, (GLsizei)m_windowHeight);


    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

}

void AI_SOKOBAN::Renderer::display(const Level& level) 
{ 
    int width,height;
    glfwGetWindowSize(&width,&height);

    int x = level.getCols()*25;
    int y = level.getRows()*25;

    if(width != m_windowWidth ||height != m_windowHeight)
    {
        reshape(width,height);
    }

    glClear(GL_COLOR_BUFFER_BIT	| GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, m_windowWidth, 0, m_windowHeight);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    DrawLevel(level);

    glfwSwapBuffers();
}

void AI_SOKOBAN::Renderer::DrawLevel(const Level& level)
{

    const int box_size = 32;
    std::pair<int,int> player = level.getPlayer();

    for(int i=0; i < level.getRows();i++)
    {
        for(int j=0; j < level.getCols();j++)
        {
            glPushMatrix();
            glTranslatef(0,m_windowHeight,0);
            glTranslatef(j*box_size,-i*box_size,0);


            player = level.getPlayer();

            if(j == player.first && i == player.second){
                //Player
                glColor3f(0.0,0.0,1.0);
            }
            else if(level.at(j,i) == '#')
            {
                //Wall
                glColor3f(0.5,0.5,0.5);

                /*
                GLuint textureid;
                glGenTextures(1, &textureid);
                glBindTexture(GL_TEXTURE_2D, textureid);
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 25, 25, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
                */
            }
            else if(level.at(j,i) == '@')
            {
                //Player
                glColor3f(0.0,0.0,1.0);
            }
            else if(level.at(j,i) == '$')
            {
                //Box
                glColor3f(0.5,0.0,0.0);
            }
            else if(level.at(j,i) == ' ')
            {
                //Air
                glColor3f(1.0,1.0,1.0);
            }
            else if(level.at(j,i) == '.')
            {
                //Goal Square
                glColor3f(1.0,0.0,0.0);
            }
            else if(level.at(j,i) == '*')
            {
                //Box on goal square 
                glColor3f(0.0,1.0,0.0);
            }
            else if(level.at(j,i) == '+')
            {
                //Player on goal square
                glColor3f(0.3,0.0,1.0);
            }
            else if(level.at(j,i) == 'X')
            {
                //Player on X square
                glColor3f(0.1,0.1,0.1);
            }
            else if(level.at(j,i) == '-')
            {
                //Player on - square
                glColor3f(0.2,0.2,0.2);
            }

            glBegin(GL_QUADS);
            glVertex2f(0, 0);
            glVertex2f(box_size, 0);
            glVertex2f(box_size, -box_size);
            glVertex2f(0, -box_size);
            glEnd();

            glPopMatrix();
        }
    }
}

